<?php			
/**
 * @version		$Id: links.php 2011.08.11 thierry $
 * @package		admin.models
 * @subpackage	com_projid
 * @copyright	Copyright (C) 2011 Projid All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.modellist');

class ProjidModellinks extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'birt_name', '#__projid_birt.birt_name',
				'ordering', '#__projid_birt.ordering',
			);
		}

		parent::__construct($config);
	}
	
	
		/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @return	void
	 * @since	1.6
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		$app = JFactory::getApplication('administrator');

		$birt_name = JRequest::getVar('birt_name',null);
		if ($birt_name) {
			if ($birt_name != $app->getUserState($this->context.'.filter.birt_name')) {
				$app->setUserState($this->context.'.filter.birt_name', $birt_name);
				JRequest::setVar('limitstart', 0);
			}
		}
		else {
			$birt_name = $app->getUserState($this->context.'.filter.birt_name');

			if (!$birt_name) {
				$birt_name = $this->getDefaultBirtName();
			}
		}

		$this->setState('filter.birt_name', $birt_name);
		// Component parameters.
		$params	= JComponentHelper::getParams('com_projid');
		$this->setState('params', $params);
		$report_id = $this->getUserStateFromRequest($this->context.'.filter.report_id', 'filter_report_id', 0, 'int');
		$this->setState('filter.report_id',	$report_id);
		
		// List state information.
		parent::populateState('ordering', 'asc');
	}

	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.birt_name');
		$id	.= ':'.$this->getState('filter.report_id');
		return parent::getStoreId($id);
	}
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	 protected function getDefaultBirtName()
	{
		// Create a new query object.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true)
			->select('birt_name')
			->from('#__projid_birt')
			->order('birt_name');
		$db->setQuery($query, 0, 1);
		$birtName = $db->loadResult();

		return $birtName;
	}
	
	protected function getListQuery() 
	{
		// Create a new query object.
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$user	= JFactory::getUser();
		$app	= JFactory::getApplication();
		
		// Select some fields
		$query->select('link_id,report_id, param_id, birt_name, param_name, ordering');
		// From the hello table
		$query->from('#__projid_birt');
		$query->leftjoin('#__projid_report_param  on #__projid_birt.id_birt=#__projid_report_param.report_id');
		$query->leftjoin('#__projid_birt_parameter  on #__projid_report_param.param_id=#__projid_birt_parameter.id_param');
		$query->order('birt_name, ordering');
		
		// Filter on the birt name.
		if ($birt_name = $this->getState('filter.birt_name')) {
			$query->where('#__projid_birt.birt_name = '.$db->quote($birt_name));
		}
		
		return $query;
	}
	public function getParams()
	{

        $db = JFactory::getDBO();
		$query = $db->getQuery(true);

		// Select some fields
		$query->select('id_birt,param_name');
		// From the hello table
		$query->from('#__projid_birt');
		$query->leftjoin('#__projid_report_param  on #__projid_birt.id_birt=#__projid_report_param.report_id');
		$query->leftjoin('#__projid_birt_parameter  on #__projid_report_param.param_id=#__projid_birt_parameter.id_param');
		//$query->where('#__projid_report_param.report_id=#__projid_birt.id_birt');
		
        $items  = $this->_getList((string) $query, $this->getState('list.start'), $this->getState('list.limit'));
 
        // Check for a database error.
        if ($this->_db->getErrorNum()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
        }
 
        // Add the items to the internal cache.
     //   $this->_cache[$store] = $items;
 return $items;
      //  return $this->_cache[$store];

}

	
}
